package zry.Achievement.Reward;

import zry.Search.SearchCriteriaReward;
import zry.Util.DBUtil;
import Connection.*;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class RewardDAOImpl extends DAOBase implements RewardDAO {
    private static final String REWARD_INSERT_SQL = "INSERT INTO reward_table(achievement_id,reward_name,reward_type,reward_level,reward_ranking,reward_time,supporting) VALUES(?,?,?,?,?,?,?)";
    private static final String REWARD_UPDATE_SQL = "UPDATE reward_table SET reward_name=?,reward_type=?,reward_level=?,reward_ranking=?,reward_time=?,supporting=? WHERE achievement_id=?";
    private static final String REWARD_SELECT_SQL = "SELECT * FROM reward_table WHERE achievement_id=?";
    private static final String REWARD_SELECT_ALL_SQL = "SELECT * FROM reward_table";

    @Override
    public boolean addReward(Reward reward) {
        Connection con = null;
        try {
            con = getConnection();
            PreparedStatement psmt = con.prepareStatement(REWARD_INSERT_SQL);
            psmt.setString(1, DBUtil.fixSqlFieldValue(reward.getA_id()));
            psmt.setString(2, DBUtil.fixSqlFieldValue(reward.getR_name()));
            psmt.setString(3, DBUtil.fixSqlFieldValue(reward.getR_type()));
            psmt.setString(4, DBUtil.fixSqlFieldValue(reward.getR_level()));
            psmt.setString(5, DBUtil.fixSqlFieldValue(reward.getR_ranking()));
            psmt.setDate(6, reward.getR_time());
            psmt.setString(7, DBUtil.fixSqlFieldValue(reward.getR_supporting()));
            psmt.executeUpdate();
            psmt.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    @Override
    public boolean updateReward(Reward reward) {
        Connection con = null;
        try {
            con = getConnection();
            PreparedStatement psmt = con.prepareStatement(REWARD_UPDATE_SQL);
            psmt.setString(1, DBUtil.fixSqlFieldValue(reward.getR_name()));
            psmt.setString(2, DBUtil.fixSqlFieldValue(reward.getR_type()));
            psmt.setString(3, DBUtil.fixSqlFieldValue(reward.getR_level()));
            psmt.setString(4, DBUtil.fixSqlFieldValue(reward.getR_ranking()));
            psmt.setDate(5, reward.getR_time());
            psmt.setString(6, DBUtil.fixSqlFieldValue(reward.getR_supporting()));
            psmt.setString(7, DBUtil.fixSqlFieldValue(reward.getA_id()));
            psmt.executeUpdate();
            psmt.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    @Override
    public Reward getReward(String achievement_id) {
        Reward reward = null;
        Connection con = null;
        try {
            con = getConnection();
            PreparedStatement psmt = con.prepareStatement(REWARD_SELECT_SQL);
            psmt.setString(1, achievement_id);
            ResultSet rs = psmt.executeQuery();
            if (rs.next()) {
                reward = new Reward();
                reward.setA_id(rs.getString("achievement_id"));
                reward.setR_name(rs.getString("reward_name"));
                reward.setR_type(rs.getString("reward_type"));
                reward.setR_level(rs.getString("reward_level"));
                reward.setR_ranking(rs.getString("reward_ranking"));
                reward.setR_time(rs.getDate("reward_time"));
                reward.setR_supporting(rs.getString("supporting"));
            }
            rs.close();
            psmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return reward;
    }

    public List<Reward> getAllRewards() {
        List<Reward> Rewards = new ArrayList<>();
        Connection con = null;
        try {
            con = getConnection();
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(REWARD_SELECT_ALL_SQL);
            while (rs.next()) {
                Reward reward = new Reward();
                reward.setA_id(rs.getString("achievement_id"));
                reward.setR_name(rs.getString("reward_name"));
                reward.setR_type(rs.getString("reward_type"));
                reward.setR_level(rs.getString("reward_level"));
                reward.setR_ranking(rs.getString("reward_ranking"));
                reward.setR_time(rs.getDate("reward_time"));
                reward.setR_supporting(rs.getString("supporting"));
                Rewards.add(reward);
            }
            rs.close();
            stmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return Rewards;
    }

    @Override
    public List<Reward> findRewards(SearchCriteriaReward searchCriteriaReward) {
        List<Reward> Rewards = new ArrayList<>();
        Connection con = null;
        try {
            // 构建SQL语句
            StringBuilder sqlBuilder = new StringBuilder("SELECT * FROM reward_table WHERE 1=1");
            if (searchCriteriaReward.getA_id() != null) {
                sqlBuilder.append(" AND achievement_id LIKE ?");
            }
            if (searchCriteriaReward.getR_name() != null) {
                sqlBuilder.append(" AND reward_name LIKE ?");
            }
            if (searchCriteriaReward.getR_type() != null) {
                sqlBuilder.append(" AND reward_type LIKE ?");
            }
            if (searchCriteriaReward.getR_level() != null) {
                sqlBuilder.append(" AND reward_level LIKE ?");
            }
            if (searchCriteriaReward.getR_ranking() != null) {
                sqlBuilder.append(" AND reward_ranking LIKE ?");
            }
            if (searchCriteriaReward.getR_time() != null) {
                sqlBuilder.append(" AND reward_time LIKE ?");
            }
            if (searchCriteriaReward.getR_supporting() != null) {
                sqlBuilder.append(" AND supporting LIKE ?");
            }
            String sql = sqlBuilder.toString();

            // 执行查询
            con = getConnection();
            PreparedStatement psmt = con.prepareStatement(sql);
            int index = 1;
            if (searchCriteriaReward.getA_id() != null) {
                psmt.setString(index++, searchCriteriaReward.getA_id());
            }
            if (searchCriteriaReward.getR_name() != null) {
                psmt.setString(index++, searchCriteriaReward.getR_name());
            }
            if (searchCriteriaReward.getR_type() != null) {
                psmt.setString(index++, searchCriteriaReward.getR_type());
            }
            if (searchCriteriaReward.getR_level() != null) {
                psmt.setString(index++, searchCriteriaReward.getR_level());
            }
            if (searchCriteriaReward.getR_ranking() != null) {
                psmt.setString(index++, searchCriteriaReward.getR_ranking());
            }
            if (searchCriteriaReward.getR_time() != null) {
                psmt.setDate(index++, searchCriteriaReward.getR_time());
            }
            if (searchCriteriaReward.getR_supporting() != null) {
                psmt.setString(index++, searchCriteriaReward.getR_supporting());
            }
            ResultSet rs = psmt.executeQuery();
            while (rs.next()) {
                String a_id = rs.getString("achievement_id");
                String r_name = rs.getString("reward_name");
                String r_type = rs.getString("reward_type");
                String r_level = rs.getString("reward_level");
                String r_ranking = rs.getString("reward_ranking");
                Date r_time = rs.getDate("reward_time");
                String r_supporting = rs.getString("supporting");
                Reward reward = new Reward(a_id, r_name, r_type, r_level, r_ranking, r_time, r_supporting);
                Rewards.add(reward);
            }
            rs.close();
            psmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return Rewards;
    }
}
